﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TreeView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            TreeNode treeNode = new TreeNode("根节点");
            treeView1.Nodes.Add(treeNode);
        }

        private void AddChildNode()
        {
            if (treeView1.SelectedNode == null)
            {
                MessageBox.Show("选择一个节点");

            }
            else
            {
                if (textBox1.Text != "")
                {
                    TreeNode tmp;
                    tmp = new TreeNode(textBox1.Text);
                    treeView1.SelectedNode.Nodes.Add(tmp);
                    treeView1.SelectedNode = tmp;
                    treeView1.ExpandAll();
                }
            }
        }

        private void AddParent()
        {
            try
            {
                if (treeView1.SelectedNode == null)
                {
                    MessageBox.Show("选择一个节点");

                }
                else
                {
                    if (textBox1.Text != "")
                    {
                        TreeNode tmp;
                        tmp = new TreeNode(textBox1.Text);
                        treeView1.SelectedNode.Parent.Nodes.Add(tmp);
                        treeView1.SelectedNode = treeView1.SelectedNode.Parent;
                        treeView1.ExpandAll();
                    }
                }
            }
            catch (Exception)
            {
                TreeNode tmp;
                tmp = new TreeNode("根节点");
                treeView1.Nodes.Add(tmp);
            }
            
        }

        private void ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AddChildNode();
        }

        private void ToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            AddParent();
        }

        private void ToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            if(treeView1.SelectedNode.Nodes.Count == 0)
            {
                treeView1.SelectedNode.Remove();
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            treeView1.SelectedNode.Expand();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            treeView1.SelectedNode=treeView1.Nodes[0];
            treeView1.SelectedNode.ExpandAll();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            treeView1.SelectedNode = treeView1.Nodes[0];
            treeView1.SelectedNode.Collapse();
        }
    }
}
